﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>@ViewBag.ProjectName-水之云出品</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="@ViewBag.LogoIcon">
    <link rel="stylesheet" href="~/lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="~/css/login.css" media="all">
</head>
<body style="background-image: url(/images/mes.jpeg);">
    @*动画特效*@
@*    <div id="starsBox"></div>*@
    <div class="layui-container">
        <div class="admin-login-background">
            <div class="layui-tab login-form" lay-filter="demo">
                <div class="logo-title">
                    <h1>@ViewBag.ProjectName</h1>
                </div>
                <ul class="layui-tab-title">
                    <li class="layui-this">账号密码登录</li>
                    <li>钉钉登录</li>
                </ul>
                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show">
                        <div class="layui-form">
                            <form class="layui-form" action="">
                                <div class="layui-form-item">
                                    <label class="layui-icon layui-icon-username" for="username"></label>
                                    <input type="text" id="username" lay-verify="required|account" placeholder="用户名" autocomplete="off" class="layui-input" value="@ViewBag.UserName">
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-icon layui-icon-password" for="password"></label>
                                    <input type="password" id="password" lay-verify="required|password" placeholder="密码" autocomplete="off" class="layui-input" value="@ViewBag.Password">
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-icon layui-icon-vercode" for="captcha"></label>
                                    <div class="code">
                                        <input id="captcha" type="text" name="captcha" lay-verify="required|captcha" placeholder="图形验证码" autocomplete="off" class="layui-input verification captcha">
                                        <canvas id="canvas" width="100" height="43"></canvas>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <span>
                                        <input type="checkbox" name="remember_user" id="remember_user" lay-skin="primary" style="vertical-align:middle;" value="true">
                                        <span style="color:#FFFFCE;vertical-align:middle;">记住密码</span>
                                    </span>
                                </div>
                                <div class="layui-form-item">
                                    <div class="login_tips"></div>
                                    <button id="login_button" class="layui-btn layui-btn-fluid" style="background-color:#FFFFCE;color:orange" lay-submit="" lay-filter="login">登 入</button>
                                </div>
                            </form>
                        </div>
                    </div>
                    <div class="layui-tab-item">
                        <div id="login_container">

                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="~/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
    <script src="~/js/cookie/jquery.cookie.js"></script>
    <script src="~/js/md5/base64.js"></script>
    <script src="~/js/md5/jquery.md5.js"></script>
    <script src="~/lib/layui/layui.js" charset="utf-8"></script>
    <script src="~/js/login.js?v=@WaterCloud.Code.GlobalContext.GetVersion()"></script>
    <script src="~/js/framework-ui.js?v=@WaterCloud.Code.GlobalContext.GetVersion()"></script>
    <script src="https://g.alicdn.com/dingding/dinglogin/0.0.5/ddLogin.js"></script>
    <script>
        var appid = '@ViewBag.DingLoginAppId';
        var url = encodeURIComponent('@ViewBag.Url');
        //判断登录
        var ua = navigator.userAgent.toLowerCase();
        var isWeixin = ua.indexOf("micromessenger") != -1;
        if (isWeixin) {
        }
        else if (window.navigator.userAgent.includes("DingTalk")) {
            window.location.href = 'https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=' + appid + '&response_type=code&scope=snsapi_auth&state=STATE&redirect_uri=' + url;
        }
        //钉钉扫码登录
        var goto = encodeURIComponent('https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=' + appid + '&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=' + url);
        if (typeof DDLogin == 'function') {
            var obj = DDLogin({
                id: "login_container",//这里需要你在自己的页面定义一个HTML标签并设置id，例如<div id="login_container"></div>或<span id="login_container"></span>
                goto: goto, //请参考注释里的方式
                style: "border:none;background-color:#FFFFFF;",
                width: "300",
                height: "300"
            });
        }
        var handleMessage = function (event) {
            var origin = event.origin;
            console.log("origin", event.origin);
            if (origin == "https://login.dingtalk.com") { //判断是否来自ddLogin扫码事件。
                var loginTmpCode = event.data;
                //获取到loginTmpCode后就可以在这里构造跳转链接进行跳转了
                layer.msg("扫码登入中...", { icon: 6, shade: 0.1, anim: 5, time: 1000 }, function () {
                    //获取到loginTmpCode后就可以在这里构造跳转链接进行跳转了 get 参数 appid 是 从创建扫码登录应用授权中获取，替换成自己创建的appid
                    window.location.href = "https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=appid&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=" +
                        url +
                        "&loginTmpCode=" +
                        loginTmpCode;
                });
            }
        };
        if (typeof window.addEventListener != 'undefined') {
            window.addEventListener('message', handleMessage, false);
        } else if (typeof window.attachEvent != 'undefined') {
            window.attachEvent('onmessage', handleMessage);
        }
        (function ($) {
            var show_num = [];
            $.login = {
                formMessage: function (msg) {
                    $('.login_tips').find('.tips_msg').remove();
                    $('.login_tips').append('<div class="tips_msg"><i class="fa fa-question-circle"></i>' + msg + '</div>');
                },
                init: function () {
                    $("#canvas").click(function () {
                        draw(show_num);
                    });
                    var login_error = top.$.cookie('WaterCloud_login_error');
                    if (login_error != null) {
                        switch (login_error) {
                            case "overdue":
                                $.login.formMessage('系统登录已超时,请重新登录');
                                break;
                            case "OnLine":
                                $.login.formMessage('您的帐号已在其它地方登录,请重新登录');
                                break;
                            case "-1":
                                $.login.formMessage('系统未知错误,请重新登录');
                                break;
                        }
                        top.$.cookie('WaterCloud_login_error', '', { path: "/", expires: -1 });
                    }
                },
                checkLogin: function () {
                    $.ajax({
                        url: "/BoardLogin/CheckLoginState?v=" + new Date().Format("yyyy-MM-dd hh:mm:ss"),
                        type: "post",
                        async: false,
                        dataType: "json",
                        success: function (data) {
                            if (data.state == "success") {
                                 window.location.href = '@Url.Content("~/BoardHome/Index")';
                            }
                        }
                    });
                }
            };
            $(function () {
                $.login.init();
                $.login.checkLogin();
                $("#canvas").click();
                var userData =JSON.parse(localStorage.getItem('currentBoard'));
                if (!!userData) {
                    $.ajax({
                        url: "/BoardLogin/CheckLogin?v=" + new Date().Format("yyyy-MM-dd hh:mm:ss"),
                        data: { username: userData.username, localurl: location.host, password: userData.password },
                        type: "post",
                        dataType: "json",
                        success: function (data) {$.trim($('#username').val())
                            if (data.state == "success") {
                                $("#login_button").empty();
                                $("#login_button").html("登录成功，正在跳转...");
                                window.location.href = '@Url.Content("~/BoardHome/Index")';
                            } else {
                                $("#login_button").empty();
                                $("#login_button").removeAttr('disabled').html("登 入");
                                draw(show_num);
                                layer.msg(data.message);
                                $.login.formMessage(data.message);
                            }
                        },
                        error: function () {
                            $("#login_button").empty();
                            $("#login_button").removeAttr('disabled').html("登 入");
                            draw(show_num);
                            layer.msg("系统未知错误,请重新登录");
                            $.login.formMessage("系统未知错误,请重新登录");
                        }
                    });
                }
                if ($.cookie("board_remember_user")) {
                    $("#remember_user").prop("checked", true);
                    $("#username").val($.cookie("username"));
                    $("#password").val(Base64.decode($.cookie("password")));
                }
            });
            function saveUserInfo() {
                if ($("#remember_user").prop("checked") == true) {
                    var user_name = $("#username").val();
                    var user_password = $("#password").val();
                    $.cookie("board_remember_user", "true", {
                        expires: 7
                    }); // 存储一个带7天期限的 cookie
                    $.cookie("board_username", user_name, {
                        expires: 7
                    }); // 存储一个带7天期限的 cookie
                    $.cookie("board_password", Base64.encode(user_password), {
                        expires: 7
                    }); // 存储一个带7天期限的 cookie
                } else {
                    $.cookie("board_remember_user", "false", {
                        expires: -1
                    }); // 删除 cookie
                    $.cookie("board_username", '', {
                        expires: -1
                    });
                    $.cookie("board_password", '', {
                        expires: -1
                    });
                }
            }
            layui.use(['form','element'], function () {
                var form = layui.form,
                    element = layui.element,
                    layer = layui.layer;
                // 登录过期的时候，跳出ifram框架
                if (top.location != self.location) top.location = self.location;
                // 进行登录操作
                form.on('submit(login)', function (data) {
                    saveUserInfo();
                    data = data.field;
                    if (data.username == '') {
                        layer.msg('用户名不能为空');
                        return false;
                    }
                    if (data.password == '') {
                        layer.msg('密码不能为空');
                        return false;
                    }
                    var num = show_num.join("");
                    if (data.captcha == '') {
                        layer.msg('验证码不能为空');
                        return false;
                    }
                    else if (num != data.captcha.toLowerCase()) {
                        layer.msg('验证码错误');
                        draw(show_num)
                        return false;
                    }
                    $("#login_button").empty();
                    $("#login_button").attr('disabled', 'disabled').html("loading...");
                    $.ajax({
                        url: "/BoardLogin/CheckLogin?v=" + new Date().Format("yyyy-MM-dd hh:mm:ss"),
                        data: { username: $.trim($('#username').val()), localurl: location.host, password: $.trim($('#password').val()) },
                        type: "post",
                        dataType: "json",
                        success: function (data) {$.trim($('#username').val())
                            if (data.state == "success") {
                                localStorage.setItem('currentBoard', JSON.stringify({ username: $.trim($('#username').val()), password: $.trim($('#password').val())}));
                                $("#login_button").empty();
                                $("#login_button").html("登录成功，正在跳转...");
                                window.location.href = '@Url.Content("~/BoardHome/Index")';
                            } else {
                                $("#login_button").empty();
                                $("#login_button").removeAttr('disabled').html("登 入");
                                draw(show_num);
                                layer.msg(data.message);
                                $.login.formMessage(data.message);
                            }
                        },
                        error: function () {
                            $("#login_button").empty();
                            $("#login_button").removeAttr('disabled').html("登 入");
                            draw(show_num);
                            layer.msg("系统未知错误,请重新登录");
                            $.login.formMessage("系统未知错误,请重新登录");
                        }

                    });

                    return false;
                });
            });
        })(jQuery);
    </script>
</body>
</html>